﻿using System;
using System.Windows.Forms;
using Google.API.Search;
using System.Collections.Generic;

namespace BotDetectorApp
{
    public partial class Form1 : Form
    {
        string mesajEroare = "Va rugam introduceti un text intre 10 si 135 caractere maxim";
        string botConfirmation = "Este bot";
        string botNot = "NU este bot";

        public Form1()
        {
            InitializeComponent();
        }

        private void isBotExactButton_Click(object sender, EventArgs e)
        {
            string botText = '"' + botTextArea.Text + '"';
            if (ValidateText())
            {
                var results = PopulateSearchList(botText);
                BotDetector botDetector = new BotDetector();
                if (botDetector.AnalyzeAfterExactPhrase(botTextArea.Text, results))
                {
                    MessageBox.Show(botConfirmation);
                }
                else
                {
                    MessageBox.Show(botNot);
                }
            }
            else
            {
                MessageBox.Show(mesajEroare);
            }
        }
        private IList<IWebResult> PopulateSearchList(string botText)
        {
            searchList.Items.Clear();
            var client = new GwebSearchClient("http://www.google.com");
            var results = client.Search(botText, 100);
            searchList.Items.Add("Numar de rezultate: " + results.Count);
            searchList.Items.Add(string.Empty);
            foreach (var webResult in results)
            {
                searchList.Items.Add(webResult.Title);
                searchList.Items.Add(webResult.Url);
                searchList.Items.Add(webResult.Content);
                searchList.Items.Add("--------------");
            }
            return results;
        }
        private bool ValidateText()
        {
            string text = botTextArea.Text;
            if (!string.IsNullOrEmpty(text) && 
                (text.Length <= 135) &&
                (text.Length >= 10))
            {
                return true;
            }
            return false;
        }
        private void searchAfterSentencesBtn_Click(object sender, EventArgs e)
        {
            string text = botTextArea.Text;
            if (!string.IsNullOrEmpty(text) && (text.Length >= 10))
            {
                var results = PopulateSearchList(text);
                BotDetector botDetector = new BotDetector();
                if (botDetector.AnalyzeAfterSentences(text, results))
                {
                    MessageBox.Show(botConfirmation);
                }
                else
                {
                    MessageBox.Show(botNot);
                }
            }
            else
            {
                MessageBox.Show("Introduceti un text de mai mult de 10 caractere");
            }
        }

        private void searchAfterKeyWordsBtn_Click(object sender, EventArgs e)
        {
            if (ValidateText())
            {
                var results = PopulateSearchList(botTextArea.Text);
                BotDetector botDetector = new BotDetector();
                if (botDetector.AnalyzeAfterKeyWords(botTextArea.Text, results))
                {
                    MessageBox.Show(botConfirmation);
                }
                else
                {
                    MessageBox.Show(botNot);
                }
            }
            else
            {
                MessageBox.Show(mesajEroare);
            }
        }

    }
}
